﻿using System.Collections.Generic;
using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using VSCleaner.Properties;
using VSCleaner.Common;
using System.IO;
using System.Windows;

namespace VSCleaner.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        private bool isCleanAvailable;
        /// <summary>
        /// Property that represents whether Clean Button is enabled.
        /// </summary>
        public bool IsCleanAvailable
        {
            get { return isCleanAvailable; }
            set
            {
                isCleanAvailable = value;
                RaisePropertyChanged("IsCleanAvailable");
            }
        }

        private ObservableCollection<string> folderList;
        /// <summary>
        /// Property that represents ObservableCollection of string as folder list.
        /// </summary>
        public ObservableCollection<string> FolderList
        {
            get { return folderList; }
            set
            {
                folderList = value;
                RaisePropertyChanged("FolderList");
            }
        }

        private ICommand addFolderCommand;
        /// <summary>
        /// Add Folder Command
        /// </summary>
        public ICommand AddFolderCommand
        {
            get { return addFolderCommand; }
            set
            {
                addFolderCommand = value;
                RaisePropertyChanged("AddFolderCommand");
            }
        }

        private ICommand cleanCommand;
        /// <summary>
        /// Clean Command
        /// </summary>
        public ICommand CleanCommand
        {
            get { return cleanCommand; }
            set
            {
                cleanCommand = value;
                RaisePropertyChanged("CleanCommand");
            }
        }

        private List<string> deleteFolderList;
        /// <summary>
        /// Property that represents Folderlist to be deleted
        /// </summary>
        public List<string> DeleteFolderList
        {
            get { return deleteFolderList; }
            set
            {
                deleteFolderList = value;
                RaisePropertyChanged("DeleteFolderList");
            }
        }

        private List<string> deleteFileList;
        /// <summary>
        /// Property that represents Files to be deleted
        /// </summary>
        public List<string> DeleteFileList
        {
            get { return deleteFileList; }
            set
            {
                deleteFileList = value;
                RaisePropertyChanged("DeleteFileList");
            }
        }

        private Visibility cleanInProgressVisibility;
        /// <summary>
        /// Visibility of the in progress grid
        /// </summary>
        public Visibility CleanInProgressVisibility
        {
            get { return cleanInProgressVisibility; }
            set
            {
                cleanInProgressVisibility = value;
                RaisePropertyChanged("CleanInProgressVisibility");
            }
        }

        private int progressValue;
        /// <summary>
        /// Progress Value of Progress Bar
        /// </summary>
        public int ProgressValue
        {
            get { return progressValue; }
            set
            {
                progressValue = value;
                RaisePropertyChanged("ProgressValue");
            }
        }

        /// <summary>
        /// Default Constructor
        /// </summary>
        public MainWindowViewModel()
        {
            AddFolderCommand = new RelayCommand(AddFolder);
            CleanCommand = new RelayCommand(Clean);
            LoadFromSettings();
            CleanInProgressVisibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Cleans the VS Temp Files
        /// </summary>
        private void Clean()
        {
            CleanInProgressVisibility = Visibility.Visible;
            DeleteFolderList = new List<string>();
            DeleteFileList = new List<string>();

            ProgressValue = 10;

            //Step 1
            foreach (var folder in FolderList)
            {
                var tempSubFolders = Directory.GetDirectories(folder);

                foreach (var item in tempSubFolders)
                {
                    if (item.ToLower().Contains("\\bin") || 
                        item.ToLower().Contains("\\obj") || 
                        item.ToLower().Contains("\\clientbin"))
                    {
                        DeleteFolderList.Add(item);
                    }
                }
            }
            ProgressValue = 40;

            //Step 2
            if (DeleteFolderList.Count > 0)
            {
                foreach (var folder in DeleteFolderList)
                {
                    DeleteFileList.AddRange(Directory.GetFiles(folder));
                }
            }
            ProgressValue = 85;

            //Step 3
            foreach (var filePath in DeleteFileList)
            {
                if (File.Exists(filePath))
                    File.Delete(filePath);
            }
            ProgressValue = 100;
            CleanInProgressVisibility = Visibility.Collapsed;

            System.Windows.MessageBox.Show("VS Folders are cleaned.", "Clean Finished", MessageBoxButton.OK);
        }

        /// <summary>
        /// Adds a Folder to FolderList Collection
        /// </summary>
        private void AddFolder()
        {
            var folderDialog = new FolderBrowserDialog();
            folderDialog.ShowNewFolderButton = false;
            System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                var selectedFolder = folderDialog.SelectedPath;
                if (!FolderList.Contains(selectedFolder))
                {
                    FolderList.Add(selectedFolder);
                }
                SaveInSettings();
            }

            CheckIfCleanAvailable();
        }

        /// <summary>
        /// Loads Settings Data
        /// </summary>
        private void LoadFromSettings()
        {
            var folders = Settings.Default.Folders;
            FolderList = new ObservableCollection<string>();

            if (folders != null)
            {
                foreach (var item in folders)
                {
                    FolderList.Add(item); 
                }
            }

            CheckIfCleanAvailable();
        }

        /// <summary>
        /// Saves Settings Data
        /// </summary>
        public void SaveInSettings()
        {
            Settings.Default.Folders = FolderList.ToStringCollection();
            Settings.Default.Save();
        }

        /// <summary>
        /// Checks if Clean Buttons should be enabled
        /// </summary>
        public void CheckIfCleanAvailable()
        {
            if (FolderList.Count > 0)
                IsCleanAvailable = true;
            else
                IsCleanAvailable = false;
        }

    }
}
